<?php
class Survey extends Action {
    
    private $startDate;
    private $stopDate;
    private $requiresLogIn = false;
    
    public function __construct( ) {
        parent::__construct();
        $this->type = Action::Survey;
    }
	
    public function load($parameters) {
        if (!parent::load($parameters)) {
        return false;
        }        
    
        extract($parameters);
        
        if (!isset($start)) {
        $this->loadError = "Start date not set!";
        return false;
        }
        
        if (!isset($stop)) {
        $this->loadError = "Stop date not set!";
        return false;
        }

        $startDate = Utilities::getTimestamp($start);
        $stopDate = Utilities::getTimestamp($stop);
	    
        if ($startDate <= 0) {
            $this->loadError = "Invalid start date!";
            return false;            
        }

        if ($stopDate <= 0) {
            $this->loadError = "Invalid stop date!";
            return false;            
        }
        
        if ($startDate > $stopDate) {
            $this->loadError = "Start date is after stop date!";
            return false;            
        }
        
        if (time() - $stopDate > 86400) {
            $this->loadError = "Stop date can't be in the past!";
            return false;                        
        }
        
        if (!isset($loginRequired)) {
            $this->requiresLogIn = 0;
        }
        else {
            $this->requiresLogIn = 1;
        }
        
        $this->startDate = date("y-m-d", $startDate);
        $this->stopDate = date("y-m-d", $stopDate);
        
	return true;
    }
    
    public function getStartDate() {
        return $this->startDate;
    }
    
    public function setStartDate($startDate) {
        $this->startDate=$startDate;
    }
    
    public function getStopDate() {
        return $this->stopDate;
    }
    
    public function setStopDate($stopDate) {
        $this->stopDate=$stopDate;
    }
    
    public function getRequiresLogIn() {
        return $this->requiresLogIn;
    }
    
    public function setRequiresLogIn($requiresLogIn) {
        $this->requiresLogIn=$requiresLogIn;
    }  
    
    public function getJsonArray() {
        $ar = parent::getJsonArray();
        $ar2 = array("type" => $this->type,
                     "start" => date("d-m-Y", $this->startDate),
                     "stop" => date("d-m-Y", $this->stopDate),
                     "requiresLogIn" => $this->requiresLogIn  ? 'true' : 'false'
                     );
        return array_merge($ar, $ar2);
    }
}
?>